package com.wjw.service;

import com.wjw.mapper.PavementDamageTableMapper;
import com.wjw.model.Pavement_damage_table;
import com.wjw.utils.GetSqlSessionUtils;
import com.wjw.viewmodel.Facility_damage_table_view;
import com.wjw.viewmodel.Pavement_damage_table_view;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PavementDamageTableService {


//    private SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
//    private PavementDamageTableMapper mapper=sqlsession.getMapper(PavementDamageTableMapper.class);


    public List<Pavement_damage_table_view> findAll(){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        PavementDamageTableMapper mapper=sqlsession.getMapper(PavementDamageTableMapper.class);
        try {
            List<Pavement_damage_table> all = mapper.findAll();
            List<Pavement_damage_table_view> list= new ArrayList<Pavement_damage_table_view>();
            for(Pavement_damage_table e:all){
                Pavement_damage_table_view k = new Pavement_damage_table_view();
                k.setEmployee_inspect_name(mapper.findEmployeeById(e.getEmployee_inspect_id()).getEmployee_name());
                k.setPavement_damage_area(e.getPavement_damage_area());
                k.setPavement_damage_date(e.getPavement_damage_date());
                k.setPavement_damage_density(e.getPavement_damage_density());
                k.setPavement_damage_id(e.getPavement_damage_id());
                k.setPavement_damage_individual(e.getPavement_damage_individual());
                k.setPavement_damage_inspectarea(e.getPavement_damage_inspectarea());
                k.setPavement_damage_remarks(e.getPavement_damage_remarks());
                k.setPavement_damage_start(e.getPavement_damage_start());
                k.setRoad_basic_id(e.getRoad_basic_id());
                k.setRoad_basic_name3(mapper.findRoadById(e.getRoad_basic_id()).getRoad_basic_name());
                k.setRoad_damage_id(e.getRoad_damage_id());
                k.setRoad_type_name(mapper.findRoadTypeById(e.getRoad_type_id()).getRoad_type_name());
                list.add(k);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public List<Pavement_damage_table_view> findByPage(int page,int limit){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        PavementDamageTableMapper mapper=sqlsession.getMapper(PavementDamageTableMapper.class);
        try {
            int start=(page-1)*limit;
            List<Pavement_damage_table> all = mapper.findByPage(start,limit);
            List<Pavement_damage_table_view> list= new ArrayList<Pavement_damage_table_view>();
            for(Pavement_damage_table e:all){
                Pavement_damage_table_view k = new Pavement_damage_table_view();
                k.setEmployee_inspect_name(mapper.findEmployeeById(e.getEmployee_inspect_id()).getEmployee_name());
                k.setPavement_damage_area(e.getPavement_damage_area());
                k.setPavement_damage_date(e.getPavement_damage_date());
                k.setPavement_damage_density(e.getPavement_damage_density());
                k.setPavement_damage_id(e.getPavement_damage_id());
                k.setPavement_damage_individual(e.getPavement_damage_individual());
                k.setPavement_damage_inspectarea(e.getPavement_damage_inspectarea());
                k.setPavement_damage_remarks(e.getPavement_damage_remarks());
                k.setPavement_damage_start(e.getPavement_damage_start());
                k.setRoad_basic_id(e.getRoad_basic_id());
                k.setRoad_basic_name3(mapper.findRoadById(e.getRoad_basic_id()).getRoad_basic_name());
                k.setRoad_damage_id(e.getRoad_damage_id());
                k.setRoad_type_name(mapper.findRoadTypeById(e.getRoad_type_id()).getRoad_type_name());
                list.add(k);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public boolean add(Pavement_damage_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        PavementDamageTableMapper mapper=sqlsession.getMapper(PavementDamageTableMapper.class);
        try {
            Pavement_damage_table k = new Pavement_damage_table();
            k.setEmployee_inspect_id(mapper.findEmployeeByName(e.getEmployee_inspect_name()).getEmployee_id());
            k.setPavement_damage_area(e.getPavement_damage_area());
            k.setPavement_damage_date(e.getPavement_damage_date());
            k.setPavement_damage_density(e.getPavement_damage_density());
            k.setPavement_damage_id(e.getPavement_damage_id());
            k.setPavement_damage_individual(e.getPavement_damage_individual());
            k.setPavement_damage_inspectarea(e.getPavement_damage_inspectarea());
            k.setPavement_damage_remarks(e.getPavement_damage_remarks());
            k.setPavement_damage_start(e.getPavement_damage_start());
            k.setRoad_basic_id(mapper.findRoadByName(e.getRoad_basic_name3()).getRoad_basic_id());
            k.setRoad_basic_name3(e.getRoad_basic_name3());
            k.setRoad_damage_id(e.getRoad_damage_id());
            k.setRoad_type_id(mapper.findRoadTypeByName(e.getRoad_type_name()).getRoad_type_id());
            mapper.add(k);
//            mapper.update1(e.getRoad_basic_id());
//            mapper.update2(e.getRoad_basic_id());
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean delete(int id){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        PavementDamageTableMapper mapper=sqlsession.getMapper(PavementDamageTableMapper.class);
        try {
            mapper.delete(id);
            sqlsession.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean change(Pavement_damage_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        PavementDamageTableMapper mapper=sqlsession.getMapper(PavementDamageTableMapper.class);
        try {
            Pavement_damage_table k = new Pavement_damage_table();
            k.setEmployee_inspect_id(mapper.findEmployeeByName(e.getEmployee_inspect_name()).getEmployee_id());
            k.setPavement_damage_area(e.getPavement_damage_area());
            k.setPavement_damage_date(e.getPavement_damage_date());
            k.setPavement_damage_density(e.getPavement_damage_density());
            k.setPavement_damage_id(e.getPavement_damage_id());
            k.setPavement_damage_individual(e.getPavement_damage_individual());
            k.setPavement_damage_inspectarea(e.getPavement_damage_inspectarea());
            k.setPavement_damage_remarks(e.getPavement_damage_remarks());
            k.setPavement_damage_start(e.getPavement_damage_start());
            k.setRoad_basic_id(mapper.findRoadByName(e.getRoad_basic_name3()).getRoad_basic_id());
            k.setRoad_basic_name3(e.getRoad_basic_name3());
            k.setRoad_damage_id(e.getRoad_damage_id());
            k.setRoad_type_id(mapper.findRoadTypeByName(e.getRoad_type_name()).getRoad_type_id());
            mapper.change(k);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }


}
